package flc.ast.fragment.home;

import android.text.method.ScrollingMovementMethod;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProvider;

import com.stark.calculator.science.CalKey;
import com.stark.calculator.science.ExpressionViewModel;
import com.stark.calculator.util.CalPrefUtil;

import flc.ast.R;
import flc.ast.databinding.FragmentOridinaryBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

public class OrdinaryFragment extends BaseNoModelFragment<FragmentOridinaryBinding> {
    private ExpressionViewModel viewModel;
    //是否为科学计算器的标志
    private boolean isVoiceEnabled = CalPrefUtil.isCalVoiceEnabled();


    @Override
    protected int onCreate() {
        return R.layout.fragment_oridinary;
    }

    @Override
    protected void initView() {
        //设置按钮监听器按钮
        TextView[] standardTextViews = new TextView[]{mDataBinding.num0, mDataBinding.num1, mDataBinding.num2, mDataBinding.num3, mDataBinding.num4, mDataBinding.num5, mDataBinding.num6, mDataBinding.num7, mDataBinding.num8, mDataBinding.num9, mDataBinding.btnDot, mDataBinding.btnEqual, mDataBinding.btnAdd, mDataBinding.btnMinus, mDataBinding.btnMultiply, mDataBinding.btnDivide, mDataBinding.btnClear, mDataBinding.leftBracket, mDataBinding.rightBracket};
        //为每个按钮添加点击事件
        for (TextView text : standardTextViews) {
            if (text != null) {
                text.setOnClickListener(this);
            }
        }

        mDataBinding.ShowText.setMovementMethod(new ScrollingMovementMethod());
        mDataBinding.ShowText.setScrollbarFadingEnabled(false);
        mDataBinding.ShowText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mDataBinding.ShowText.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

        mDataBinding.llDelete.setOnClickListener(this);
        viewModel = new ViewModelProvider(this).get(ExpressionViewModel.class);

        viewModel.getContentLiveData().observe(this, expression -> mDataBinding.ShowText.setText(expression));
        viewModel.getCalRetLiveData().observe(this, ret -> mDataBinding.resultText.setText(ret));
    }

    @Override
    protected void onClickCallback(View v) {
        Object tag = v.getTag();
        if (tag == null) return;

        if (tag instanceof String) {
            CalKey calKey = CalKey.getCalKeyByValue((String) tag);
            if (calKey == null) return;
            viewModel.handleClickCalKey(calKey);
            if (calKey.getValue() == "=") {
                mDataBinding.ShowText.scrollTo(0, 0);
            } else {
                // 设置内容后，滚动到最底部
                mDataBinding.ShowText.post(() -> {
                    int scrollAmount = mDataBinding.ShowText.getLayout().getLineTop(mDataBinding.ShowText.getLineCount()) - mDataBinding.ShowText.getHeight();
                    if (scrollAmount > 0) {
                        mDataBinding.ShowText.scrollTo(0, scrollAmount);
                    } else {
                        mDataBinding.ShowText.scrollTo(0, 0);
                    }
                });
            }
        }
    }


    @Override
    protected void initData() {

    }
}